package com.chamber.business.mqtt.config;

import com.chamber.business.mqtt.service.MqttService;
import com.chamber.business.mqtt.service.impl.MqttMessageHandler;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class MqttMessageHandle implements MqttCallback {

    private MqttFactory mqttFactory;

    @Autowired
    private MqttService mqttService;

    @Autowired
    private MqttMessageHandler mqttMessageHandler;

    @Autowired
    private MqttConfigurationProperties mqttConfigurationProperties;

    @Override
    public void connectionLost(Throwable throwable) {
        log.error("MQTT连接丢失", throwable);
        // 可以在这里添加重连逻辑
    }

    @Override
    public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
        log.info("订阅到消息-主题{}，内容{}", topic, mqttMessage);

        // 使用消息处理器处理不同类型的消息
        mqttMessageHandler.handleMessage(topic, mqttMessage.toString());
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        log.debug("消息发送完成");
    }
}
